package com.safetys.co.gateway;

import com.assembly.co.safetys.SpringContextUtil;
import com.safetys.co.gateway.entity.Settings;
import com.safetys.co.gateway.service.GatewayService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Configuration
@ConditionalOnClass({GatewayService.class})
@EnableConfigurationProperties(GatewayProperties.class)
public class GatewayAutoConfiguration {

	@Autowired
	private GatewayProperties gatewayProperties;
	
	@Autowired
	private SpringContextUtil springContextUtil;
	
	@Bean
	public GatewayService gatewayService () {
		
		log.info("APISixRoutesService Profile is {}", springContextUtil.getActiveProfile());
		
		Settings settings = new Settings();
		BeanUtils.copyProperties(gatewayProperties, settings);
		GatewayService gatewayService = GatewayService.getInstance(settings);
		return gatewayService;
	}

	@Bean
	public com.safetys.co.gateway.v2.service.GatewayService gatewayService_v2 () {

		log.info("APISixRoutesService Profile is {}", springContextUtil.getActiveProfile());

		Settings settings = new Settings();
		BeanUtils.copyProperties(gatewayProperties, settings);
		com.safetys.co.gateway.v2.service.GatewayService gatewayService = com.safetys.co.gateway.v2.service.GatewayService.getInstance(settings);
		return gatewayService;
	}
}
